/**
 * Copyright &copy; 2016-2019 <a href="https://www.yidian.com">一点科技股份有限公司</a> All rights reserved.
 */
package com.pack.BaseUtil;

import java.util.Date;


import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.pack.ToolEntity.IdGen;
import com.pack.bean.User;

/**
 * 数据Entity类
 */
public abstract class DataEntity<T> extends BaseEntity<T> {

	private static final long serialVersionUID = 1L;
	
	protected String remarks;	// 备注
	protected User createBy;	// 创建者
	protected Date createDate;	// 创建日期
	protected User updateBy;	// 更新者
	protected Date updateDate;	// 更新日期
	protected String delFlag; 	// 删除标记（0：正常；1：删除；2：审核）
	
	protected String systemType; 	// 系统类型
	protected String role; 	// 当前VO的数据权限设置
	
	public DataEntity() {
		super();
		this.delFlag = DEL_FLAG_NORMAL;
	}
	
	public DataEntity(String id) {
		super(id);
	}
	
	/**
	 * 插入之前执行方法，需要手动调用
	 */
	@Override
	public void preInsert(){
		// 不限制ID为UUID，调用setIsNewRecord()使用自定义ID
		if (!this.isNewRecord){
			setId(IdGen.uuid());
		}
		User user = null;
		if (user == null || StringUtils.isBlank(user.getId())) {
			user = this.currentUser;
		}
		if (user != null && StringUtils.isNotBlank(user.getId())){
			this.updateBy = user;
			this.createBy = user;
		}
		this.updateDate = new Date();
		this.createDate = this.updateDate;
	}
	
	/**
	 * 更新之前执行方法，需要手动调用
	 */
	@Override
	public void preUpdate(){
//		User user = UserUtils.getUser();
		User user = null;

		if (user == null) {
			user = this.currentUser;
		}
		if (StringUtils.isNotBlank(user.getId())){
			this.updateBy = user;
		}
		
		this.updateDate = new Date();
	}
	
	public String getRemarks() {
		return remarks;
	}

	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}
	
	@JsonIgnore
	public User getCreateBy() {
		return createBy;
	}

	public void setCreateBy(User createBy) {
		this.createBy = createBy;
	}

	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	@JsonIgnore
	public User getUpdateBy() {
		return updateBy;
	}

	public void setUpdateBy(User updateBy) {
		this.updateBy = updateBy;
	}

	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	public Date getUpdateDate() {
		return updateDate;
	}

	public void setUpdateDate(Date updateDate) {
		this.updateDate = updateDate;
	}

	@JsonIgnore
	public String getDelFlag() {
		return delFlag;
	}

	public void setDelFlag(String delFlag) {
		this.delFlag = delFlag;
	}
	
	public String getSystemType() {
		return systemType;
	}

	public void setSystemType(String systemType) {
		this.systemType = systemType;
	}
	
	public void setCurrentUser(User currentUser) {
		this.currentUser = currentUser;
//		bpmDataScopeFilter(this.role);
	}

//	/**
//	 * 业务数据范围过滤（根据实际业务情况进行数据过滤）
//	 */
//	public void bpmDataScopeFilter(String role) {
//		this.role = role;
//
//		User user = this.getCurrentUser();
//
//		// 如果是超级管理员，则不过滤数据
//		if (user.isAdmin()) {
//			return;
//		}
//
//		if (StringUtils.isBlank(role)) {
//			return;
//		}
//
//		// 权限过滤标志
//		String bpmDataFilterRole1 = "";
//		String bpmDataFilterRole2 = "";
//		String bpmDataFilterRole3 = "";
//		String bpmDataFilterRole4 = "";
//		String bpmDataFilterRole5 = "";
//		// 获取配置信息
//		String roleJson = "{" + role + "}";
//		Map<String, String> roleMap = (Map<String, String>) JsonMapper.fromJsonString(roleJson, Map.class);
//		if (roleMap != null) {
//			bpmDataFilterRole1 = String.valueOf(roleMap.get("role1"));
//			bpmDataFilterRole2 = String.valueOf(roleMap.get("role2"));
//			bpmDataFilterRole3 = String.valueOf(roleMap.get("role3"));
//			bpmDataFilterRole4 = String.valueOf(roleMap.get("role4"));
//			bpmDataFilterRole5 = String.valueOf(roleMap.get("role5"));
//		}
//
//		StringBuilder sqlString = new StringBuilder();
//		StringBuilder sqlStringJoin = new StringBuilder();
//		StringBuilder sqlStringJoin2 = new StringBuilder();
//		StringBuilder sqlStringJoin3 = new StringBuilder();
//
//		// 1、添加用户权限过滤
//		if (StringUtils.isNotBlank(bpmDataFilterRole1)) { // 用户权限过滤
//			if (bpmDataFilterRole1.indexOf(BpmConstant.DICT_LP_USER_ROLE_10) != -1
//					&& BpmUtils.isLpShipperUser(user)) { // 货主
//				sqlString.append(" AND a.SHIPPER_ID = '" + user.getId() + "' ");
//			} else if (bpmDataFilterRole1.indexOf(BpmConstant.DICT_LP_USER_ROLE_20) != -1
//					&& BpmUtils.isLpCarrierUser(user)) { // 承运商
//				sqlString.append(" AND a.CARRIER_USER_ID = '" + user.getId() + "' ");
//			} else if (bpmDataFilterRole1.indexOf(BpmConstant.DICT_LP_USER_ROLE_30) != -1
//					&& BpmUtils.isLpDriverUser(user)) { // 司机
//				sqlString.append(" AND a.DRIVER_ID = '" + user.getId() + "' ");
//			} else if (bpmDataFilterRole1.indexOf(BpmConstant.DICT_LP_USER_ROLE_99) != -1
//					&& BpmUtils.isSaleman(user)) { // 业务员
//				sqlString.append(" AND a.SALESMAN_ID = '" + user.getId() + "' ");
//			} else if ("all".equals(bpmDataFilterRole1)
//					&& user != null && StringUtils.isNoneBlank(user.getId())){
//				sqlString.append(" AND a.USER_ID = '" + user.getId() + "' ");
//			}
//		}
//		// 2、货物类型权限过滤
//		if (StringUtils.isNotBlank(bpmDataFilterRole2)) { // 用户权限过滤
//			if (bpmDataFilterRole2.indexOf(BpmConstant.DICT_LP_USER_ROLE_10) != -1
//					&& BpmUtils.isLpShipperUser(user)) { // 货主
//				sqlString.append(" AND A.CARGO_TYPE IN (SELECT CARGO_TYPE FROM T_SYS_SALESMAN_CARGO SC WHERE SC.SALESMAN_ID = '" + user.getId() + "' AND SC.DEL_FLAG = '" + DEL_FLAG_NORMAL + "') ");
//			} else if (bpmDataFilterRole2.indexOf(BpmConstant.DICT_LP_USER_ROLE_20) != -1
//					&& BpmUtils.isLpCarrierUser(user)) { // 承运商
//				sqlString.append(" AND A.CARGO_TYPE IN (SELECT CARGO_TYPE FROM T_SYS_SALESMAN_CARGO SC WHERE SC.SALESMAN_ID = '" + user.getId() + "' AND SC.DEL_FLAG = '" + DEL_FLAG_NORMAL + "') ");
//			} else if (bpmDataFilterRole2.indexOf(BpmConstant.DICT_LP_USER_ROLE_30) != -1
//					&& BpmUtils.isLpDriverUser(user)) { // 司机
//				sqlString.append(" AND A.CARGO_TYPE IN (SELECT CARGO_TYPE FROM T_SYS_SALESMAN_CARGO SC WHERE SC.SALESMAN_ID = '" + user.getId() + "' AND SC.DEL_FLAG = '" + DEL_FLAG_NORMAL + "') ");
//			} else if (bpmDataFilterRole2.indexOf(BpmConstant.DICT_LP_USER_ROLE_99) != -1
//					&& BpmUtils.isSaleman(user)) { // 业务员
//				sqlString.append(" AND A.CARGO_TYPE IN (SELECT CARGO_TYPE FROM T_SYS_SALESMAN_CARGO SC WHERE SC.SALESMAN_ID = '" + user.getId() + "' AND SC.DEL_FLAG = '" + DEL_FLAG_NORMAL + "') ");
//			}
//		}
//		// 3、发货区域
//		if (StringUtils.isNotBlank(bpmDataFilterRole3)) { // 用户权限过滤
//			if (bpmDataFilterRole3.indexOf(BpmConstant.DICT_LP_USER_ROLE_10) != -1
//					&& BpmUtils.isLpShipperUser(user)) { // 货主
//				sqlString.append(" AND A.START_AREA IN (SELECT START_AREA FROM T_SYS_SALESMAN_AREA sa WHERE sa.SALESMAN_ID = '" + user.getId() + "' AND sa.DEL_FLAG = '" + DEL_FLAG_NORMAL + "') ");
//			} else if (bpmDataFilterRole3.indexOf(BpmConstant.DICT_LP_USER_ROLE_20) != -1
//					&& BpmUtils.isLpCarrierUser(user)) { // 承运商
//				sqlString.append(" AND A.START_AREA IN (SELECT START_AREA FROM T_SYS_SALESMAN_AREA sa WHERE sa.SALESMAN_ID = '" + user.getId() + "' AND sa.DEL_FLAG = '" + DEL_FLAG_NORMAL + "') ");
//			} else if (bpmDataFilterRole3.indexOf(BpmConstant.DICT_LP_USER_ROLE_30) != -1
//					&& BpmUtils.isLpDriverUser(user)) { // 司机
//				sqlString.append(" AND A.START_AREA IN (SELECT START_AREA FROM T_SYS_SALESMAN_AREA sa WHERE sa.SALESMAN_ID = '" + user.getId() + "' AND sa.DEL_FLAG = '" + DEL_FLAG_NORMAL + "') ");
//			} else if (bpmDataFilterRole3.indexOf(BpmConstant.DICT_LP_USER_ROLE_99) != -1
//					&& BpmUtils.isSaleman(user)) { // 业务员
//				sqlString.append(" AND A.START_AREA IN (SELECT START_AREA FROM T_SYS_SALESMAN_AREA sa WHERE sa.SALESMAN_ID = '" + user.getId() + "' AND sa.DEL_FLAG = '" + DEL_FLAG_NORMAL + "') ");
//			}
//		}
//
//		// 4、业务员所属承运商、货主权限过滤
//		if (StringUtils.isNotBlank(bpmDataFilterRole4) && BpmUtils.isSaleman(user)) { // 用户权限过滤
//			if (BpmConstant.DICT_LP_USER_ROLE_10.equals(bpmDataFilterRole4)) { // 货主
//				sqlString.append(" AND A.SHIPPER_ID IN (SELECT SHIPPER_ID FROM (SELECT SHIPPER_ID FROM T_SYS_SALESMAN_SHIPPER sc WHERE sc.SALESMAN_ID = '" + user.getId() + "' AND sc.DEL_FLAG = '" + DEL_FLAG_NORMAL + "') AS tmp )");
//			} else if (BpmConstant.DICT_LP_USER_ROLE_20.equals(bpmDataFilterRole4)) { // 承运商
//				sqlString.append(" AND A.CARRIER_USER_ID IN (SELECT CARRIER_USER_ID FROM (SELECT CARRIER_USER_ID FROM T_SYS_SALESMAN_CARRIER sc WHERE sc.SALESMAN_ID = '" + user.getId() + "' AND sc.DEL_FLAG = '" + DEL_FLAG_NORMAL + "') AS tmp )");
//			} else if (bpmDataFilterRole4.contains(BpmConstant.DICT_LP_USER_ROLE_10)
//					&& bpmDataFilterRole4.contains(BpmConstant.DICT_LP_USER_ROLE_20)) {
//				sqlString.append(" AND A.SHIPPER_ID IN (SELECT SHIPPER_ID FROM (SELECT SHIPPER_ID FROM T_SYS_SALESMAN_SHIPPER sc WHERE sc.SALESMAN_ID = '" + user.getId() + "' AND sc.DEL_FLAG = '" + DEL_FLAG_NORMAL + "') AS tmp )");
//				sqlString.append(" AND A.CARRIER_USER_ID IN (SELECT CARRIER_USER_ID FROM ( SELECT CARRIER_USER_ID FROM T_SYS_SALESMAN_CARRIER sc WHERE sc.SALESMAN_ID = '" + user.getId() + "' AND sc.DEL_FLAG = '" + DEL_FLAG_NORMAL + "') AS tmp)");
//			}else if (bpmDataFilterRole4.contains(BpmConstant.DICT_LP_USER_ROLE_20)
//					&& bpmDataFilterRole4.contains(BpmConstant.DICT_LP_USER_ROLE_80)) {
//				sqlStringJoin3.append(" JOIN T_SYS_SALESMAN_CARRIER sc ON sc.`CARRIER_USER_ID` = a.`CARRIER_USER_ID` AND  sc.SALESMAN_ID = '" + user.getId() + "' AND sc.DEL_FLAG = '" + DEL_FLAG_NORMAL + "'");
//			}
//		}
//
//		// 5、业务员财务信息权限过滤
//		if (StringUtils.isNotBlank(bpmDataFilterRole5) && BpmUtils.isSaleman(user)) { // 用户权限过滤
//			if (BpmConstant.SALESMAN_FINANCE_AUTH_PAYMENT.equals(bpmDataFilterRole5)) { // 付款单
//				sqlString.append(" AND A.CARRIER_USER_ID IN (SELECT CARRIER_USER_ID FROM (SELECT CARRIER_USER_ID FROM T_SYS_SALESMAN_CARRIER sc WHERE sc.SALESMAN_ID = '" + user.getId() + "' AND sc.DEL_FLAG = '" + DEL_FLAG_NORMAL + "') AS tmp )");
//			} else if (BpmConstant.SALESMAN_FINANCE_AUTH_RECEIPT.equals(bpmDataFilterRole5)) { // 收款单
//				sqlString.append(" AND A.SHIPPER_ID IN (SELECT SHIPPER_ID FROM (SELECT SHIPPER_ID FROM T_SYS_SALESMAN_SHIPPER sc WHERE sc.SALESMAN_ID = '" + user.getId() + "' AND sc.DEL_FLAG = '" + DEL_FLAG_NORMAL + "')  AS tmp)");
//			} else if (BpmConstant.SALESMAN_FINANCE_AUTH_STATEMENT.equals(bpmDataFilterRole5)) { // 对账单
//				sqlStringJoin2.append(" JOIN T_SYS_SALESMAN_SHIPPER ss ON ( ss.SHIPPER_ID = a.RECIPIENT_ID  AND ss.SALESMAN_ID =  '" + user.getId() + "' AND ss.del_flag='" + DEL_FLAG_NORMAL + "')");
//				sqlStringJoin.append(" JOIN T_SYS_SALESMAN_CARRIER sc ON ( sc.carrier_user_id = a.RECIPIENT_ID AND  sc.SALESMAN_ID =  '" + user.getId() + "' AND sc.del_flag='" + DEL_FLAG_NORMAL + "')");
//			} else if (BpmConstant.SALESMAN_FINANCE_AUTH_STATEMENT_DETAIL.equals(bpmDataFilterRole5)) { // 对账单明细
//				sqlString.append(" AND A.SHIPPER_ID IN (SELECT SHIPPER_ID FROM (SELECT SHIPPER_ID FROM T_SYS_SALESMAN_SHIPPER sc WHERE sc.SALESMAN_ID = '" + user.getId() + "' AND sc.DEL_FLAG = '" + DEL_FLAG_NORMAL + "')  AS tmp)");
//				sqlString.append(" AND A.CARRIER_USER_ID IN (SELECT CARRIER_USER_ID FROM (SELECT CARRIER_USER_ID FROM T_SYS_SALESMAN_CARRIER sc WHERE sc.SALESMAN_ID = '" + user.getId() + "' AND sc.DEL_FLAG = '" + DEL_FLAG_NORMAL + "') AS tmp) ");
//			} else if (BpmConstant.SALESMAN_FINANCE_AUTH_PAYMENT_VERIFY.equals(bpmDataFilterRole5)) { // 付款核销单
//				sqlString.append(" AND A.CARRIER_USER_ID IN (SELECT CARRIER_USER_ID FROM (SELECT CARRIER_USER_ID FROM T_SYS_SALESMAN_CARRIER sc WHERE sc.SALESMAN_ID = '" + user.getId() + "' AND sc.DEL_FLAG = '" + DEL_FLAG_NORMAL + "')  AS tmp)");
//			} else if (BpmConstant.SALESMAN_FINANCE_AUTH_RECEIPT_VERIFY.equals(bpmDataFilterRole5)) { // 收款核销单
//				sqlString.append(" AND A.SHIPPER_ID IN (SELECT SHIPPER_ID FROM (SELECT SHIPPER_ID FROM T_SYS_SALESMAN_SHIPPER sc WHERE sc.SALESMAN_ID = '" + user.getId() + "' AND sc.DEL_FLAG = '" + DEL_FLAG_NORMAL + "')  AS tmp)");
//			} else if (BpmConstant.SALESMAN_FINANCE_AUTH_ADJUST_MEMO.equals(bpmDataFilterRole5)) { // 调整单
//				sqlString.append(" AND A.SHIPPER_ID IN (SELECT SHIPPER_ID FROM (SELECT SHIPPER_ID FROM T_SYS_SALESMAN_SHIPPER sc WHERE sc.SALESMAN_ID = '" + user.getId() + "' AND sc.DEL_FLAG = '" + DEL_FLAG_NORMAL + "')  AS tmp)");
//				sqlString.append(" AND A.CARRIER_USER_ID IN (SELECT CARRIER_USER_ID FROM (SELECT CARRIER_USER_ID FROM T_SYS_SALESMAN_CARRIER sc WHERE sc.SALESMAN_ID = '" + user.getId() + "' AND sc.DEL_FLAG = '" + DEL_FLAG_NORMAL + "')   AS tmp)");
//			} else if (BpmConstant.SALESMAN_FINANCE_AUTH_ADJUST_PRICE.equals(bpmDataFilterRole5)) { // 调价单
//				sqlString.append(" AND A.SHIPPER_ID IN (SELECT SHIPPER_ID FROM (SELECT SHIPPER_ID FROM T_SYS_SALESMAN_SHIPPER sc WHERE sc.SALESMAN_ID = '" + user.getId() + "' AND sc.DEL_FLAG = '" + DEL_FLAG_NORMAL + "')   AS tmp)");
//				sqlString.append(" AND A.CARRIER_USER_ID IN (SELECT CARRIER_USER_ID FROM (SELECT CARRIER_USER_ID FROM T_SYS_SALESMAN_CARRIER sc WHERE sc.SALESMAN_ID = '" + user.getId() + "' AND sc.DEL_FLAG = '" + DEL_FLAG_NORMAL + "')  AS tmp) ");
//			}
//		}
//
//		// sqlMap的键值，调用方法：${sqlMap.bdsf}
//		this.getSqlMap().put("bdsf", sqlString.toString());
//		this.getSqlMap().put("bdsfJoin", sqlStringJoin.toString());
//		this.getSqlMap().put("bdsfJoin2", sqlStringJoin2.toString());
//		this.getSqlMap().put("bdsfJoin3SettlePayable", sqlStringJoin3.toString());
//
//	}
}
